// ignore_for_file: must_be_immutable
import 'dart:math';

import 'package:decimal/decimal.dart';
import 'package:flutter/material.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';
import 'package:junyeyishu/widgets/QGButton.dart';

class QGNFTBuyAlert extends StatefulWidget {
  dynamic data;
  int maxNumber;
  Function(int count) onBuy;

  QGNFTBuyAlert(
      {super.key, required this.onBuy, this.data, required this.maxNumber});
  @override
  State<QGNFTBuyAlert> createState() => _QGNFTBuyAlertState();

  static show(context, data, maxNumber, onBuy) {
    showDialog(
        useSafeArea: false,
        context: context,
        builder: (context) {
          return QGNFTBuyAlert(data: data, maxNumber: maxNumber, onBuy: onBuy);
        });
  }
}

class _QGNFTBuyAlertState extends State<QGNFTBuyAlert> {
  int count = 1;
  String price = "";
  final decimal = Decimal.parse;

  // int maxNumber = 999999;

  var controller = TextEditingController();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    controller.text = "1";
    price = widget.data["price"] ?? "";
    controller.addListener(() {
      count = int.parse(controller.text);
      count = min(widget.maxNumber, count);
      count = max(1, count);

      if (count > widget.maxNumber) {
        controller.text = "${widget.maxNumber}";
      } else if (count < 1) {
        controller.text = "1";
      }

      updatePrice();
    });
  }

  updatePrice() {
    price = (decimal(widget.data["price"] ?? "") * decimal("$count"))
        .toStringAsFixed(2);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pop();
      },
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: GestureDetector(
          onTap: () {
            unfocus(context);
            controller.text = "$count";
          },
          child: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Container(
                padding:
                    EdgeInsets.only(top: 20.rpx, bottom: max(10, kSafeBottom)),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10.rpx),
                  color: Color(0xFF192126),
                ),
                child: Column(children: [
                  // Text(
                  //   '确认抢购',
                  //   style: TextStyle(
                  //       color: Color(0xFF111111),
                  //       fontSize: 16.rpx,
                  //       fontWeight: FontWeight.normal),
                  // ),
                  SizedBox(height: 20.rpx),
                  buildContent(),
                  SizedBox(height: 10.rpx),
                  buildBottom(),
                ]),
              ),
            ],
          ),
        ),
      ),
    );
  }

  buildContent() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 10.rpx),
      padding: EdgeInsets.all(15.rpx),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.rpx),
        color: Color(0xF192126),
      ),
      child: Row(children: [
        ClipRRect(
          borderRadius: BorderRadius.circular(5.rpx),
          child: QGImage(
              url: widget.data["thumb_img"], width: 80.rpx, height: 80.rpx),
        ),
        SizedBox(width:10.rpx),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                widget.data["name"] ?? "",
                style: TextStyle(
                    color: Color(0xFFffffff),
                    fontSize: 14.rpx,
                    fontWeight: FontWeight.normal),
              ),
              SizedBox(height: 15.rpx),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    '购买数量',
                    style: TextStyle(
                        color: Color(0xFFffffff),
                        fontSize: 12.rpx,
                        fontWeight: FontWeight.normal),
                  ),
                  buildCount(),
                ],
              ),
            ],
          ),
        ),
      ]),
    );
  }

  buildCount() {
    return Row(
      children: [
        GestureDetector(
          onTap: onSub,
          child: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(22.rpx),
              color: Color(0xFF4AE3FF),
            ),
            alignment: Alignment.center,
            width: 20.rpx,
            height: 20.rpx,
            child: Text(
              '-',
              style: TextStyle(
                  color: Color(0xFF111111),
                  fontSize: 15.rpx,
                  fontWeight: FontWeight.normal),
            ),
          ),
        ),
        SizedBox(
          width: 70.rpx,
          child: TextField(
            textAlign: TextAlign.center,
            controller: controller,
            keyboardType: TextInputType.number,
            textInputAction: TextInputAction.done,
            cursorColor: Color(0xFFffffff),
            style: TextStyle(
                color: Color(0xFFffffff),
                fontSize: 16.rpx,
                fontWeight: FontWeight.bold),
            decoration: const InputDecoration(
              enabledBorder: InputBorder.none,
              focusedBorder: InputBorder.none,
            ),
          ),
        ),
        GestureDetector(
          onTap: onAdd,
          child: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(22.rpx),
              color: Color(0xFF4AE3FF),
            ),
            alignment: Alignment.center,
            width: 20.rpx,
            height: 20.rpx,
            child: Text(
              '+',
              style: TextStyle(
                  color: Color(0xFF111111),
                  fontSize: 15.rpx,
                  fontWeight: FontWeight.normal),
            ),
          ),
        ),
      ],
    );
  }

  buildBottom() {
    return Container(
      width: kScreenWidth,
      color: Color(0xFF192126),
      padding: EdgeInsets.fromLTRB(10.rpx, 10.rpx, 10.rpx, 0.rpx),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            '￥$price',
            style: TextStyle(
                color: Color(0xFFffffff),
                fontSize: 26.rpx,
                fontWeight: FontWeight.bold),
          ),
          Flexible(
            child: Container(
                alignment: Alignment.center,
                height: 44.rpx,
                width: 210.rpx,
                child: QGButton(
                  title: "去支付",
                  enable: true,
                  onTap: onClick,
                )),
          )
        ],
      ),
    );
  }

  onAdd() {
    // unfocus(context);
    count++;
    count = min(widget.maxNumber, count);
    controller.text = "$count";
    updatePrice();
  }

  onSub() {
    // unfocus(context);
    count--;
    count = max(1, count);
    controller.text = "$count";
    updatePrice();
    // setState(() {});
  }

  onClick() {
    unfocus(context);
    widget.onBuy(count);
  }
}
